package com.example.webapi.controller;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.webapi.Utils.ResponseModel;
import com.example.webapi.Utils.WriteJson;
import com.example.webapi.entity.Listdata;
import com.example.webapi.service.ListdataService;
import com.example.webapi.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表格数据 前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/api")
public class ListdataController {
    @Resource
    ListdataService listdataService;
    @Resource
    MenuService menuService;

    public QueryWrapper<Listdata> myWrapper(Integer typeId){
        QueryWrapper<Listdata> wrapper = new QueryWrapper<>();
        wrapper.eq("typeID",typeId);
        if(typeId == 1){
            wrapper.select("id","buildingHeight","buildingWidth");
        }else if (typeId == 2){
            wrapper.select("id","landArea");
        }else if (typeId == 3){
            wrapper.select("id","facilityName","controlValue","serveScale","areaType");
        }else if (typeId == 4){
            wrapper.select("id","buildingType","unit","ratio");
        }
        return wrapper;
    }

    /**
     * 获取所有的数据库表格
     */
    @GetMapping("getTableData")
    public ResponseModel getTableData(@RequestParam Integer typeId){
        // 判断构造条件
        QueryWrapper<Listdata> wrapper = myWrapper(typeId);
        List<Map<String,Object>> tableList  = listdataService.listMaps(wrapper);
        //type=3特殊处理
//        if (typeId == 3 && tableList.size()>0){
//            Map<String,Object> map = tableList.get(0);
//            Integer id = Convert.toInt(map.get("id"));
//            Map<String,Object> menuMap = menuService.getMap(new QueryWrapper<Menu>().eq("tableId",id));
//            Menu menu = menuService.getById(Convert.toInt(menuMap.get("parentId")));
//            String facilityName = menu.getRuleName();
//            for (Map<String,Object> tableData:tableList)
//                tableData.put("facilityName",facilityName);
//        }
        return ResponseModel.success(tableList);
    }

    /**
     * 修改数据
     */
    @PostMapping("update")
    public ResponseModel update(@RequestBody JSONObject data){
        UpdateWrapper<Listdata> wrapper = new UpdateWrapper<>();
        if(data.getInt("typeId") == 1){
            wrapper.set("buildingHeight",data.getStr("buildingHeight")).set("buildingWidth",data.getStr("buildingWidth"));
        }else if (data.getInt("typeId") == 2){
            wrapper.set("landArea",data.getStr("landArea"));
        }else if (data.getInt("typeId") == 3){
            wrapper.set("controlValue",data.getStr("controlValue"));
        }else if (data.getInt("typeId") == 4){
            wrapper.set("ratio",data.getStr("ratio"));
        }
        wrapper.eq("id",data.getInt("id"));

        boolean result = listdataService.update(null,wrapper);
        if (result){
            //写入json文件
            WriteJson.wrtie(listdataService);
            return ResponseModel.success("数据修改成功");
        }else{
            return ResponseModel.fail("数据修改失败");
        }
    }
}
